package com.xiedafeng.www.view;

import com.xiedafeng.www.controller.EventController;
import com.xiedafeng.www.po.Circle;
import com.xiedafeng.www.po.Event;
import com.xiedafeng.www.po.User;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.List;

public class EventView {
    EventController eventController=new EventController();
    public void showUserEvent(User user,List<Event> eventList) {
        GridPane gridPane=new GridPane();
        event(user,eventList,gridPane);
    }
    public void showCircleEvent(User user, Circle circle,List<Event> eventList) {
        GridPane gridPane=new GridPane();
        Button createButton=new Button("发瓜");
        gridPane.add(createButton,1,0);
        createButton.setOnMouseClicked(event -> createEvent(user,circle));
        event(user,eventList,gridPane);
    }
    public void showThumbUpEvent(User user, List<Event> eventList) {
        GridPane gridPane=new GridPane();
        event(user,eventList,gridPane);
    }
    public void showCollectEvent(User user,List<Event> eventList) {
        GridPane gridPane=new GridPane();
        event(user,eventList,gridPane);
    }
    private void showEventRemark(User user, Event event) throws Exception {
        eventController.showEventRemark(user,event);
    }
    private void event(User user, List<Event> eventList, GridPane gridPane){
        Stage stage=new Stage();
        for (int i = 1; i <= eventList.size(); i++) {
            Event event=eventList.get(i-1);
            Text publisher=new Text(event.getPublisher());
            Text createtime=new Text(event.getCreatetime());
            Text content=new Text(event.getContent());
            Button thumbupButton=new Button(event.getThumbUp()+"点赞");
            Button remark=new Button(event.getRemark()+"评论");
            Button collectionButton=new Button(event.getCollect()+"收藏");
            Button delete=new Button("删除");
            Button tipoff=new Button("举报");
            gridPane.add(publisher,0,i*3);
            gridPane.add(createtime,2,i*3);
            gridPane.add(content,0,i*3+1,5,1);
            gridPane.add(thumbupButton,0,i*3+2);
            gridPane.add(remark,1,i*3+2);
            gridPane.add(collectionButton,2,i*3+2);
            gridPane.add(delete,3,i*3+2);
            gridPane.add(tipoff,4,i*3+2);
            thumbupButton.setOnMouseClicked(event1 -> {
                try {
                    thumbUp(user,event);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            collectionButton.setOnMouseClicked(event1 -> {
                try {
                    collect(user,event);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            remark.setOnMouseClicked(event1 -> {
                try {
                    showEventRemark(user,event);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            delete.setOnMouseClicked(event1 -> {
                try {
                    deleteEvent(user,event);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            tipoff.setOnMouseClicked(mouseevent -> tipOff(user,event));
        }
        gridPane.setAlignment(Pos.TOP_CENTER);
        Scene scene=new Scene(gridPane);
        stage.setScene(scene);
        stage.showAndWait();
    }
    private void createEvent(User user, Circle circle){
        eventController.createEventView(user,circle);
    }
    private void deleteEvent(User user, Event event) throws Exception {
        eventController.deleteEvent(user,event);
    }
    private void thumbUp(User user, Event event) throws Exception {
        eventController.thumbUp(user,event);
    }
    private void collect(User user,Event event) throws Exception {
        eventController.collect(user,event);
    }
    private void tipOff(User user, Event event){
        eventController.showTipOffView(user,event);
    }
}
